<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/23
 * Time: 20:02
 */

namespace app\common\controller;


use app\common\utils\JsonService;
use app\common\model\GoodsCollect as GoodsCollectModel;
class GoodsCollect extends BaseController
{
    public function create(){
        if($this->request->isAjax()){
            $post = $this->request->post();

            if(!in_array($post['type'],[0,1])){
                return JsonService::error('参数错误');
            }
            $member_id = session('member_id');

            if(!$member_id){
                return JsonService::error('请登录后操作');
            }

            $post['member_id'] = $member_id;
            if($post['type'] == 0) {
                $has = GoodsCollectModel::getOne(['member_id' => $member_id, 'type' => $post['type'],'goods_id'=>$post['goods_id']]);
                if(!empty($has)){
                    return JsonService::error('该商品已经收藏过');
                }
            }else{
                $has = GoodsCollectModel::getOne(['member_id'=>$member_id,'type'=>$post['type'],'shop_id'=>$post['shop_id']]);
                if(!empty($has)){
                    return JsonService::error('该店铺已经收藏过');
                }
            }
            GoodsCollectModel::create($post);
            return JsonService::success('收藏成功');
        }
    }

}